package city.spring.modules.material.controller;

import city.spring.modules.material.entity.MaterialCategoryEntity;
import city.spring.modules.material.entity.MaterialSettingEntity;
import city.spring.modules.material.service.MaterialCategoryService;
import city.spring.modules.material.service.MaterialSettingService;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Controller：物资类别管理
 *
 * @author 谢金婷
 * @date 2020/12/10 0026 14:20
 */
@RestController
@RequestMapping("/materialManagement")
public class MaterialManagementController extends ApiController {
    /**
     * Service：物资类别信息
     */
    private final MaterialCategoryService materialCategoryService;
    /**
     * Service：物资信息
     */
    private final MaterialSettingService materialSettingService;

    public MaterialManagementController(MaterialCategoryService materialCategoryService, MaterialSettingService materialSettingService) {
        this.materialCategoryService = materialCategoryService;
        this.materialSettingService = materialSettingService;
    }


    @GetMapping("tree")
    public Object tree() {
        List<MaterialCategoryEntity> CategoryEntities = materialCategoryService.list();
//        ListMultimap<String, MaterialSettingEntity> materialMultimap = getMaterialMultimap(true);
//        for (MaterialCategoryEntity entity : CategoryEntities) {
//            List<MaterialSettingEntity> entities = materialMultimap.get(String.valueOf(entity.getId()));
//            // 重新对列表进行排序
//            entities.sort(Comparator.comparing(MaterialSettingEntity::getGmtCreate, Date::compareTo));
//            entity.setChildren(entities);
//        }
        return success(CategoryEntities);
    }

//    @GetMapping(value = "tree", params = {"loadmaterial=true"})
//    public Object treeLoadmaterial() {
//        List<MaterialCategoryEntity> categoryEntities = materialCategoryService.list();
//        ListMultimap<String, MaterialSettingEntity> multimap = getMaterialMultimap(true);
//        categoryEntities.forEach(entity -> {
//            List<MaterialSettingEntity> entities = multimap.get(String.valueOf(entity.getId()));
//            // 重新对列表进行排序
//            entities.sort(Comparator.comparing(MaterialSettingEntity::getGmtCreate, Date::compareTo));
//            entity.setChildren(entities);
//        });
//        return success(categoryEntities);
//    }

//    /**
//     * 以 仓库ID 为键，收集每一个仓库的货架列表信息
//     *
//     * @return 每个仓库的货架列表信息
//     */
//    private ListMultimap<String, ShelfEntity> getShelfMultimap(boolean loadShellevel) {
//        // 获取所有货架列表
//        List<ShelfEntity> shelfEntities = shelfService.list();
//        // 判断是否需要加载货架层次信息
//        ListMultimap<String, ShelflevelEntity> shelflevelMultimap = null;
//        if (loadShellevel) {
//            shelflevelMultimap = getShelflevelMultimap();
//        }
//
//        BiConsumer<ListMultimap<String, ShelflevelEntity>, ShelfEntity> loadChildren = (multimap, entity) -> {
//            List<ShelflevelEntity> entities = multimap.get(entity.getId());
//            // 重新对列表进行排序
//            entities.sort(Comparator.comparing(ShelflevelEntity::getGmtCreate, Date::compareTo));
//            entity.setChildren(entities);
//        };
//
//        // 以 仓库ID 为键，收集每一个货架的货架层次列表信息
//        ListMultimap<String, ShelfEntity> multimap = ArrayListMultimap.create();
//
//        for (ShelfEntity entity : shelfEntities) {
//            if (loadShellevel) {
//                loadChildren.accept(shelflevelMultimap, entity);
//            }
//            multimap.put(String.valueOf(entity.getStorehouseId()), entity);
//        }
//        return multimap;
//    }

    /**
     * 以 货架ID 为键，收集每一个货架的货架层次列表信息
     *
     * @return 每个房间的床位列表信息
     */
    private ListMultimap<String, MaterialSettingEntity> getMaterialMultimap() {
        // 获取所有房间列表
        List<MaterialSettingEntity> materialEntities = materialSettingService.list();
        // 以 楼层ID 为键，收集每一个楼层的房间列表信息
        ListMultimap<String, MaterialSettingEntity> multimap = ArrayListMultimap.create();

        materialEntities.forEach(entity -> multimap.put(entity.getCategoryId(), entity));

        return multimap;
    }
}
